home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: merge algorithm
- Date: Tue, 27 Feb 96 22:46:48 GMT
- Organization: none
- Message-ID: <825461208snz@genesis.demon.co.uk>
- References: <312CEE69.842@onyx.idbsu.edu> <4gljrl$auj@sun001.spd.dsccc.com>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4gljrl$auj@sun001.spd.dsccc.com>
- jmccarty@spd.dsccc.com "Mike McCarty" writes:
-
- >In article <312CEE69.842@onyx.idbsu.edu>,
- >Joe E. Coffland <jcofflan@onyx.idbsu.edu> wrote:
- >)I am trying to find an algorithm to merge two sorted arrays containing
- >)n elements.
- >)ie. A[1] <= A[2]<= ... <= A[m] and A[m+1] <= A[m+2] <= ... <A[n]
- >)The algorithm must run in O(n) time and require only a small amount of
- >)fixed additional memory regardless of the size of m or n. I have reason
- >)to believe that there is such an algorithm but I have only been able to
- >)find algorithms that meet the memory restriction but run in at best
- >)O(nlogn) and are recursive (which can through some work of course be
- >)changed in to an iterative algorithm). If any one can point me to a book
- >)or any other source of information on the subject or just get me headed
- >)in the right direction it would be greatly appriciated.
-
- As far as I am aware there is no such algorithm although I'd be very
- happy to be proved wrong. One obvious O(nlogn) algorithm is heapsort.
-
- >Won't a simple exchange algorithm work? Start with pointers into the
- >left and right halves. Advance the left pointer until you find an
- >element larger than the right pointer data. Exchange the left pointer
- >data with the right pointer data. Advance the left pointer etc. When you
- >have gone through the entire data set, there is only one output array.
-
- Unfortunately this doesn't work. As soon as you do the first exchange the
- elements of the 'right' array are no longer guaranteed sorted so the
- algorithm breaks down. 'Fixing' this raises the algorithm order.
-
- This is an interesting challenge - if it was solved it would make merge
- sort a good candidate for a qsort() implementation.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-